﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;
using System.Text;
using System.Collections.Generic;
using System.Reflection;

namespace WzhModule.GM
{
    public class WzhGMControl : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
    {
        public static WzhGMControl I { get; private set; }
        public static Transform uicanvas { get; private set; }    //var uicanvas = GameObject.Find("UICanvas").transform;
        public static Camera UICamera { get; private set; }    // GameFrameEntry.GetModule<UIModule>().UICamera;
        public static List<Assembly> assemblyList;// GameFramework.Utility.AssemblyUtility.LoadCSharpAssembly();
        public static float CanvasWidth { get; private set; }
        public static float CanvasHeight { get; private set; }


        public GameObject Content;
        private GameObject MiniPanel { get { return WzhGMContent.I.MiniPanel; } }


        public static void Init(Transform uicanvas, Camera UICamera, List<Assembly> assemblyList)
        {
            if (I != null) return;
            WzhGMControl.uicanvas = uicanvas;
            WzhGMControl.UICamera = UICamera;
            WzhGMControl.assemblyList = assemblyList;
            Vector2 canvasScaler = uicanvas.GetComponent<UnityEngine.UI.CanvasScaler>().referenceResolution;
            CanvasWidth = canvasScaler.x;
            CanvasHeight = canvasScaler.y;

            string GMPrefab = Screen.width > Screen.height ? "GM_LandScape" : "GM";
            var gm = Resources.Load<WzhGMControl>(GMPrefab);
            I = Instantiate(gm, uicanvas, false);
            I.transform.localScale = Vector3.one;
            I.transform.localPosition = Vector3.zero;
            I.Content.SetActive(false);
        }
        public static void Show()
        {
            if (I == null) return;
            I.Content.SetActive(true);
            WzhGMContent.I.OnOpen();
            WzhGMContent.I.OnClickDBGButton();
        }
        private void Start()
        {
            Content.SetActive(false);

            var posx = PlayerPrefs.GetFloat("GM.positionx", MiniPanel.transform.localPosition.x);
            var posy = PlayerPrefs.GetFloat("GM.positiony", MiniPanel.transform.localPosition.y);
            MiniPanel.transform.localPosition = new Vector3(posx, posy, 0);
        }

        private void Dispose()
        {
            DestroyImmediate(this.gameObject);
            I = null;
        }

        #region Drag
        private bool InDrag = false;
        Vector2 offset = new Vector2();

        public void OnBeginDrag(PointerEventData eventData)
        {
            if (eventData.pointerEnter != MiniPanel) return;

            InDrag = true;
            var parent = MiniPanel.transform.GetComponent<RectTransform>();
            RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, eventData.position, UICamera, out var canvasPoint);
            offset = canvasPoint;

        }

        public void OnDrag(PointerEventData eventData)
        {
            if (InDrag == false) return;
            var parent = MiniPanel.transform.parent.GetComponent<RectTransform>();
            RectTransformUtility.ScreenPointToLocalPointInRectangle(parent, eventData.position, UICamera, out var canvasPoint);
            MiniPanel.transform.localPosition = canvasPoint - offset;
        }

        public void OnEndDrag(PointerEventData eventData)
        {
            if (InDrag == false) return;

            PlayerPrefs.SetFloat("GM.positionx", MiniPanel.transform.localPosition.x);
            PlayerPrefs.SetFloat("GM.positiony", MiniPanel.transform.localPosition.y);
            PlayerPrefs.Save();
            InDrag = false;
        }
        #endregion


        #region debug log
        public void Log(string str)
        {

        }
        public void Log(StringBuilder str)
        {
            Log(str.ToString());
            str.Clear();
        }
        public void Error(string str)
        {

        }

        #endregion
    }
}